/*
 * 文 件 名:  XMLReader.java
 * 版    权:  Chrhc Technologies Co., Ltd. Copyright 2015-2099,  All rights reserved
 * 描    述:  <描述>
 * 修 改 人: 姓名 xxx
 * 修改时间:  2015年11月23日
 */
package com.chrhc.projects.cpub.gen.xml;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * 
 * @author Martin3000
 *         
 */
public class IbatisConfigXMLReader
{
    static Logger log = Logger.getLogger(IbatisConfigXMLReader.class);
    
    // 配置文件名
    // private static String filename = "conf.xml";
    
    /**
     * <一句话功能简述> <功能详细描述>
     * 
     * @param fileName
     * @return
     */
    public static IbatisConfigBean getIbatisconfig(String fileName)
    {
        // Config config = new Config();
        IbatisConfigBean config = new IbatisConfigBean();
        try
        {
            File f = new File(fileName);
            if (!f.exists())
            {
                log.error("  Error : Config file doesn't exist!");
                System.exit(1);
            }
            SAXReader reader = new SAXReader();
            reader.setValidation(false);
            reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
            Document doc;
            doc = reader.read(f);
            
            Element root = doc.getRootElement();
            Element data;
            // Iterator<?> itr = root.elementIterator("context");
            
            Element context = root.element("context");
            log.debug("context::" + context);
            Element jdbc = context.element("jdbcConnection");
            log.debug("jdbc::" + jdbc);
            
            Attribute driverClass = jdbc.attribute("driverClass");
            log.debug("driverClass::" + driverClass.getText());
            config.setDriverClass(driverClass.getText());
            Attribute connectionURL = jdbc.attribute("connectionURL");
            log.debug("connectionURL::" + connectionURL.getText());
            config.setConnectionURL(connectionURL.getText());
            Attribute userId = jdbc.attribute("userId");
            log.debug("userId::" + userId.getText());
            config.setUserId(userId.getText());
            Attribute password = jdbc.attribute("password");
            log.debug("password::" + password.getText());
            config.setPassword(password.getText());
            Element javaModelGenerator = context.element("javaModelGenerator");
            Attribute modelTargetPackage = javaModelGenerator.attribute("targetPackage");
            log.debug("modelTargetPackage::" + modelTargetPackage.getText());
            config.setModelTargetPackage(modelTargetPackage.getText());
            
            Attribute modelTargetProject = javaModelGenerator.attribute("targetProject");
            log.debug("modelTargetProject::" + modelTargetProject.getText());
            // config.setTargetProject(modelTargetProject.getText());
            config.setModelTargetProject(modelTargetProject.getText());
            
            Element javaServiceGenerator = context.element("javaServiceGenerator");
            Attribute serviceTargetPackage = javaServiceGenerator.attribute("targetPackage");
            log.debug("javaServiceGenerator::" + serviceTargetPackage.getText());
            config.setServiceTargetPackage(serviceTargetPackage.getText());
            
            Attribute serviceTargetProject = javaServiceGenerator.attribute("targetProject");
            config.setServiceTargetProject(serviceTargetProject.getText());
            
            Element javaControllerGenerator = context.element("javaControllerGenerator");
            Attribute controlTargetPackage = javaControllerGenerator.attribute("targetPackage");
            log.debug("javaControlGenerator::" + controlTargetPackage.getText());
            config.setControlTargetPackage(controlTargetPackage.getText());
            
            Attribute controlTargetProject = javaControllerGenerator.attribute("targetProject");
            config.setControlTargetProject(controlTargetProject.getText());
            
            Iterator it = context.elementIterator("table");
            List<IbatisConfigTableBean> tableList = new ArrayList();
            while (it.hasNext())
            {
                IbatisConfigTableBean tableMap = new IbatisConfigTableBean();
                tableList.add(tableMap);
                data = (Element)it.next();
                Attribute tableName = data.attribute("tableName");
                log.debug("tableName::" + tableName.getText());
                tableMap.setTableName(tableName.getText());
                Attribute domainObjectName = data.attribute("domainObjectName");
                log.debug("domainObjectName::" + domainObjectName.getText());
                tableMap.setDomainObjectName(domainObjectName.getText());
                Attribute entityPackage = data.attribute("entityPackage");
                if (entityPackage != null)
                {
                    log.debug("entityPackage::" + entityPackage.getText());
                    tableMap.setEntityPackage(entityPackage.getText());
                }
                
                Attribute controllerMatchUrl = data.attribute("controllerMatchUrl");
                if (controllerMatchUrl != null)
                {
                    log.debug("controlMatchUrl::" + controllerMatchUrl.getText());
                    tableMap.setControllerMatchUrl(controllerMatchUrl.getText());
                }
                Attribute enableGenerateController = data.attribute("enableGenerateController");
                if (enableGenerateController != null)
                {
                    log.debug("enableGenerateConcole::" + enableGenerateController.getText());
                    tableMap.setEnableGenerateController(enableGenerateController.getText());
                }
                Attribute enableGenerateService = data.attribute("enableGenerateService");
                if (enableGenerateService != null)
                {
                    log.debug("enableGenerateService::" + enableGenerateService.getText());
                    tableMap.setEnableGenerateService(enableGenerateService.getText());
                }
                Attribute enableGenerateView = data.attribute("enableGenerateView");
                if (enableGenerateView != null)
                {
                    log.debug("enableGenerateView::" + enableGenerateView.getText());
                    tableMap.setEnableGenerateView(enableGenerateView.getText());
                }
                Attribute title = data.attribute("title");
                if (title != null)
                {
                    log.debug("title::" + title.getText());
                    tableMap.setTitle(title.getText());
                }
                putTableColumn(data, tableMap);
                log.debug("putTableColumn end");
            }
            log.debug("config::" + config);
            log.debug("config.setTableList::start");
            config.setTableList(tableList);
        }
        catch (Exception ex)
        {
            log.error("Error : " + ex.toString());
        }
        return config;
        
    }
    
    private static void putTableColumn(Element table, IbatisConfigTableBean tableMap)
    {
        Iterator it = table.elementIterator("colomn");
        Element col;
        List<IbatisConfigColumn> cols = new ArrayList();
        IbatisConfigColumn colsMap = null;
        
        // Map colMap = null;
        while (it.hasNext())
        {
            colsMap = new IbatisConfigColumn();
            cols.add(colsMap);
            // colMap = new HashMap();
            col = (Element)it.next();
            Attribute colName = col.attribute("name");
            // colMap.put("name", colName.getText());
            // colsMap.put(colName.getText(), colMap);
            colsMap.setName(colName.getText());
            log.debug("colName.getText()=" + colName.getText());
            log.debug("colsMap.getName()=" + colsMap.getName());
            Iterator colIt = col.elementIterator("anno");
            List annList = new ArrayList();
            while (colIt.hasNext())
            {
                Element anno = (Element)colIt.next();
                annList.add(anno.getTextTrim());
            }
            // colMap.put("annos", annList);
            colsMap.setAnnotations(annList);
        }
        
        tableMap.setColumns(cols);
    }
}
